#!/usr/bin/env python

import gtk
import gettext
from mxit import handles
from blend import mxm
import constants as iconstants
import gobject
import htmltextview


gettext.install('blendIT');

class mxmTab:

   failed = False;
   aliases = {};

   def __init__(self,blend,conv):
      self.conv = conv;
      self.blend = blend;

      conv.view = htmltextview.HtmlTextView()
      conv.view.connect('url-clicked',self.blend.gui.chatWindow.sigs.on_url_clicked);
      conv.view.set_pixels_above_lines(4);
      conv.view.set_left_margin(6);
      conv.view.set_right_margin(6);
      conv.view.set_wrap_mode(gtk.WRAP_WORD);
      buf = conv.view.get_buffer();
      buf.set_text('');
      buf.create_mark("end",buf.get_end_iter(),False);
      conv.view.show();


      self.scrollWindow = gtk.ScrolledWindow();
      self.scrollWindow.set_policy(gtk.POLICY_NEVER,gtk.POLICY_AUTOMATIC);
      self.scrollWindow.add(conv.view);
      self.scrollWindow.show();
      
      if self.openMXM():
         if self.mxm.encrypted == True:
            key = self.decryptDialog();
            if key in ('',None):
               # fail (blank, destroy)
               self.failed = True;
            else:
               # try
               if not self.mxm.decrypt(key):
                  # fail (Reason: Wrong key)
                  self.gui.createErrorMessage(self.blend.gui.chatWindow,_("The password you entered was incorrect"));
                  self.failed = True;
         self.messages = self.mxm.parse();
      else:
         # fail (Reason: could not open)
         self.gui.createErrorMessage(self.blend.gui.chatWindow,_("Could not open file %s"));
         self.failed = True;
         
               
   def parseMessages(self):
      for i in range(0,len(self.messages)):
         try:
            col = self.aliases[self.messages[i].buddy.name];
         except KeyError:
            if self.messages[i].buddy.name == self.blend.currentAccount.name:
               col = iconstants.COLOUR_Blue;
            else:
               rows = len(self.aliases);
               while rows > 10:
                  rows = rows - 10;
               if rows < 0:
                  rows = 0;
               col = iconstants.colours[int(rows)];
               self.aliases[self.messages[i].buddy.name] = col;

         self.blend.gui.chatWindow.parseMessage(self.messages[i],self.conv,False,col);


   def decryptDialog(self):
      # ENCRYPTED. Ask for Code
      dialog = gtk.Dialog(_("Decrypt File"),self.blend.gui.chatWindow,
                          gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
                          (gtk.STOCK_CANCEL, gtk.RESPONSE_REJECT,
                          gtk.STOCK_OK, gtk.RESPONSE_ACCEPT));
      dialog.set_border_width(4);
      dialog.vbox.set_spacing(8);
      entry = gtk.Entry();
      entry.set_visibility(False);
      dialog.vbox.pack_start(gtkLabel(_("This file is encrypted. Enter the password to decrypt it:")));
      dialog.vbox.pack_start(entry);
      dialog.vbox.show_all();
      response = dialog.run();
      if response == gtk.RESPONSE_ACCEPT:
         dialog.destroy();
         return entry.get_text();
      else:
         dialog.destroy();
         return None;


   def openMXM(self):
      try:
         f = open(self.conv.buddy.jid,'rb');
         data = f.read();
         f.close();

         self.mxm = mxm.mxmReader(data);
         return True;
      except IOError:
         return False;